<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Number game</title>
  </head>
  <body>
    <!-- 猜数字游戏随机100以内的数猜10次看能否猜中 -->
    <!-- 优化空间利用防抖来限制用户 -->
    <h1>猜数字游戏</h1>
    <h2>共有10次机会猜中</h2>
    <div class="form">
      <label for="guessField"
        >请猜数：
        <input type="text" class="guessField" placeholder="请输入0-100的数字" />
        <input type="submit" value="确定" class="guessSubmit" />
      </label>
    </div>
    <div class="resultParas">
      <p class="guesses"></p>
      <p class="lastResult"></p>
      <p class="lowOrHi"></p>
    </div>
    <script>
      let randomNumber = Math.floor(Math.random() * 100) + 1;
      const guesses = document.querySelector(".guesses");
      const lastResult = document.querySelector(".lastResult");
      const lowOrHi = document.querySelector(".lowOrHi");

      const guessSubmit = document.querySelector(".guessSubmit");
      const guessField = document.querySelector(".guessField");
      let guessCount = 1;
      let resetButton;
      guessSubmit.onclick = () => {
        console.log(guessField.value, "guessField.value");
        if (Number(guessField.value) <= 100 && Number(guessField.value) >= 0) {
          checkGuess();
        } else {
          guessField.value = "";
          alert("请输入0-100的数字");
        }
      };
      function checkGuess() {
        let userGuess = Number(guessField.value);
        if (guessCount === 1) {
          guesses.textContent = "上次猜的数:";
        }
        guesses.textContent += userGuess + " ";
        if (userGuess === randomNumber) {
          lastResult.textContent = "恭喜猜对了！";
          lastResult.style.backgroundColor = "green";
          lowOrHi.textContent = "";
          setGameOver();
        } else if (guessCount === 10) {
          lastResult.textContent = "Gave over";
          setGameOver();
        } else {
          lastResult.textContent = "你猜错了";
          lastResult.style.backgroundColor = "red";
          if (userGuess < randomNumber) {
            lastResult.textContent = "你猜低了";
          } else if (userGuess > randomNumber) {
            lastResult.textContent = "你猜高了";
          }
        }
        guessCount++;
        guessField.value = "";
        guessField.focus();
      }
      function setGameOver() {
        guessField.disabled = true;
        guessSubmit.disabled = true;
        resetButton = document.createElement("button");
        resetButton.textContent = "开始新游戏";
        document.body.appendChild(resetButton);
        resetButton.addEventListener("click", resetGame);
      }
      function resetGame() {
        guessCount = 1;
        const resetParas = document.querySelectorAll(".resultParas p ");
        for (let i = 0; i < resetParas.length; i++) {
          resetParas[i].textContent = "";
        }
        resetButton.parentNode.removeChild(resetButton);
        guessField.disabled = false;
        guessSubmit.disabled = false;
        guessField.value = "";
        guessField.focus();
        lastResult.style.backgroundColor = "white";
        randomNumber = Math.floor(Math.random() * 100) + 1;
      }
    </script>
  </body>
</html>
